<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 激活达标
 *
 * @icon fa fa-circle-o
 */
class TerminalActvieStands extends Backend
{

    /**
     * TerminalActvieStands模型对象
     * @var \app\common\model\TerminalActvieStands
     */
    protected $model = null;

    /**
     * user模型对象
     * @var \app\common\model\user
     */
    protected $userModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\TerminalActvieStands;
        $this->userModel = new \app\common\model\User;
        $this->view->assign("processList", $this->model->getProcessList());
        $this->view->assign("standsTypsList", $this->model->getStandsTypsList());
        $this->view->assign("excTypeList", $this->model->getExcTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        $parms = $this->request->param();

        $offset = $this->request->param("offset");
        $limit = $this->request->param("limit");

        
        $defaultWhere = ["stands_typs" => 1, "process" => 2];
        $now =  strtotime(date("Y-m-d", time()));
        $timesWhere = "end_times < " . $now;

        $total = Db::name("terminal_actvie_stands")->where($defaultWhere)->where($timesWhere)->count();
        $lists = Db::name("terminal_actvie_stands")
            ->where($timesWhere)
            ->where($defaultWhere)
            ->limit((int)$offset, (int) $limit)
            ->select();
        if ($total) {
            $pro = Db::name('product')->column("name", "id");
            foreach ($lists as $k => $v) {
                $lists[$k]['bind_times'] = date('Y-m-d', $v['bind_times']);
                $lists[$k]['end_times'] = date('Y-m-d', $v['end_times']);
                $lists[$k]["proname"] = $pro[$v['proid']];
                $lists[$k]["name"]  = $this->userModel->where("id = " . $v['uid'])->value('nickname');
            }
        }
        $result = ['total' => $total, 'rows' => $lists];
        return json($result);
    }
}
